package toolx

import (
	"errors"
)

func InArray(need interface{}, haystack interface{}) (int, error) {
	switch key := need.(type) {
	case int:
		if h, ok := haystack.([]int); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case int8:
		if h, ok := haystack.([]int8); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case int16:
		if h, ok := haystack.([]int16); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case int32:
		if h, ok := haystack.([]int32); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case int64:
		if h, ok := haystack.([]int64); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case string:
		if h, ok := haystack.([]string); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case float32:
		if h, ok := haystack.([]float32); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	case float64:
		if h, ok := haystack.([]float64); !ok {
			return -1, errors.New("类型错误")
		} else {
			for i, item := range h {
				if item == key {
					return i, nil
				}
			}
		}
	default:
		return -1, errors.New("无法判断")
	}

	return -1, nil
}

func ArrayReverseString(in []string) []string {
	for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 {
		in[i], in[j] = in[j], in[i]
	}
	return in
}

func ArrayColumn(in []map[string]string, column string) []string {
	result := []string{}
	for _, items := range in {
		for k, v := range items {
			if k == column {
				result = append(result, v)
			}
		}
	}
	return result
}
